// import type { UserInfo } from '#/store';
import { GetTokenFn,GetNewInfo } from '../api/sys/userCode';
import { doLogout,GetPermCode } from '../api/sys/user';
import { defineStore } from 'pinia';
import { store } from '../store';
import { getToken, setTokens,setCodeToken } from '../utils/cookies'

interface UserState {
  userInfo: string;
  token?: string;
  sessionTimeout?: boolean;
  name:string
}

export const useUserStore = defineStore({
  id: 'app-user',
  state: (): UserState => ({
    // user info
    userInfo: null,
    // token
    token: undefined,
    // Whether the login expired
    sessionTimeout: false,
    name:''
  }),
  getters: {
    getUserInfo(): UserInfo {
      return this.userInfo  || null
    },
    getToken(): string {
      return this.token || 'jxyjxy_token'
    },
    getSessionTimeout(): boolean {
      return !!this.sessionTimeout;
    },
    getName():string {
      return this.name || 'glodon5'
    }
  },
  actions: {
    setToken(info: string | undefined) {
      this.token = info;
      setTokens(info);
    },
    setUserInfo(info: UserInfo) {
      this.userInfo = info;
      setTokens(info);
    },
    setSessionTimeout(flag: boolean) {
      this.sessionTimeout = flag;
    },
    resetState() {
      this.userInfo = null;
      this.token = '';
      this.sessionTimeout = false;
    },
    setTokenCodeToken(info: string) {
      setCodeToken(info)
    },
    /**
     * @description: login
     */
    async login(code) {
      try {

      //   let givenName = {username: 'glodon5'}
      // const getUserNameAction = await this.getUserNameAction(givenName);
        
        const data = await GetTokenFn(code);

        // save token
        this.setTokenCodeToken(data.access_token);
        // get user info
        
        const userInfo = await this.getUserInfoAction(data);


        return userInfo;
      } catch (error) {
        return Promise.reject(error);
      }
    },
    async getUserInfoAction(params): Promise<UserInfo> {
      const userInfo = await GetNewInfo(params);
      //console.log(`userInfo===`,userInfo)
      // let givenName = {username: userInfo.loginName}
      let givenName = {username: 'glodon5'}
      const getUserNameAction = await this.getUserNameAction(givenName);
      
      return userInfo;
    },

    async getUserNameAction(params) {
      const userToken = await GetPermCode(params);
      console.log(`xxooxxooxxooxxoo`,userToken)
      this.setToken(userToken);
      return userToken;
    },

    /**
     * @description: jwt
     */
     async getJwt(params) {
      try {
        const jwt = await GetPermCode(params);
        localStorage.setItem('JWT',jwt)
       // console.log(`jxy_JWT`,jwt)
        this.setToken(jwt);
        return jwt;
      } catch {
        console.log('注销Token失败');
       }
    },


    /**
     * @description: logout
     */
    async logout(goLogin = false) {
      try {
        await doLogout();
      } catch {
        console.log('注销Token失败');
      }
      this.setToken(undefined);
      this.setSessionTimeout(false);
    },

    /**
     * @description: Confirm before logging out
     */
    confirmLoginOut() {
      
    },
  },
});

// Need to be used outside the setup
export function useUserStoreWithOut() {
  return useUserStore(store);
}
